package com.cskaoyan.controller.wx;

import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.model.admin.vo.AdminInfoBean;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.admin.vo.LoginUserData;
import com.cskaoyan.model.vx.bo.WxRegisterBo;
import com.cskaoyan.model.vx.vo.WxLoginDataVo;
import com.cskaoyan.service.wx.WxAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 11:29
 */
@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {

    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("/login")
    public BaseRespVo authLogin(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        WxLoginDataVo loginUserData = wxAuthService.wxLogin(username, password);
        if (loginUserData == null) {
            return BaseRespVo.fail(700, "账号不存在或密码错误");
        }
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("/logout")
    public BaseRespVo authLogout(HttpSession httpSession) {
        wxAuthService.wxLogout(httpSession);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("/regCaptcha")
    public BaseRespVo authRegCaptcha(@RequestBody Map map, HttpSession httpSession) {
        String mobile = (String) map.get("mobile");
        if (mobile.length() != 11) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        try {
            Long.parseLong(mobile);
        } catch (Exception e) {
            return BaseRespVo.fail(402, "参数值不对");
        }

        try {
            wxAuthService.wxAuthRegCaptcha(mobile, httpSession);
        } catch (Exception e) {
            return BaseRespVo.fail(702, "验证码未超时1分钟，不能发送");
        }
        return BaseRespVo.ok(null);
    }


    @RequestMapping("register")
    public BaseRespVo wxRegister(@RequestBody WxRegisterBo wxRegisterBo) {
        Integer res = wxAuthService.wxRegister(wxRegisterBo);
        switch (res) {
            case 1:
                return BaseRespVo.fail(704, "用户名已注册");
            case 2:
                return BaseRespVo.fail(704, "手机号已存在");
            case 3:
                return BaseRespVo.fail(702, "验证码错误");
            case 4:
                return BaseRespVo.fail(706, "手机号格式错误");
        }
        return BaseRespVo.ok(null);
    }

    @RequestMapping("/reset")
    public BaseRespVo wxRest(@RequestBody Map map) {
        String code = (String) map.get("code");
        String mobile = (String) map.get("mobile");
        String password = (String) map.get("password");
        Integer res = wxAuthService.wxReset(code, mobile, password);
        if (res == 1) {
            return BaseRespVo.fail(702, "该手机号尚未注册");
        }
        if (res == 2) {
            return BaseRespVo.fail(702, "验证码错误");
        }
        if (res == 3) {
            return BaseRespVo.fail(702, "密码长度过短(少于6位)");
        }
        return BaseRespVo.ok(null);
    }
}
